home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / earthmap / _earthmap / maps / getpar / c / alloc next >
Encoding:
Text File  |  1989-04-01  |  1.3 KB  |  51 lines

  1. /*
  2.  *    allocation with error detection
  3.  *
  4.  * modified 1/26/83  S. Levin : added unsigned coersion to agree with malloc(3)
  5.  *                              changed to NULL instead of 0
  6.  * modified 3/19/83  S. Levin : keep internal 1024 byte block for small request
  7.  *                include source for malloc inline to block missing
  8.  *                cfree() linkage references.
  9.  * modified 2/7/84   S. Levin : deleted calloc() call to reduce paging -- no
  10.  *                longer is memory necessarily zeroed out of alloc.
  11.  *
  12.  * In order to allocate an array of floating point numbers, use
  13.  * the following command in the calling routine:
  14.  *
  15.  *    float *x;
  16.  *    x = (float *) alloc(nx*sizeof(float));
  17.  *
  18.  * nx is the number of elements needed in the array. 
  19.  */
  20. #include <stdio.h>
  21. #define SMALLBLOCK -1
  22. char *alloc (size)
  23. int size;
  24. {
  25.     static char *myblock = NULL; static int bytesleft = 0;
  26.     char *ptr; extern char *malloc();
  27.  
  28.     if(size > SMALLBLOCK)
  29.        {
  30.         if ((ptr = malloc ((unsigned)size)) == NULL)
  31.         err ("can't allocate %d bytes\n",size);
  32.         return (ptr);
  33.        }
  34.     else
  35.       { 
  36.         if(bytesleft < size)
  37.            {
  38.             if ((myblock = malloc ((unsigned)SMALLBLOCK)) == NULL)
  39.            err ("can't allocate %d bytes\n",size);
  40.         bytesleft = SMALLBLOCK - size;
  41.            }
  42.        else
  43.            {
  44.         bytesleft -= size;
  45.            }
  46.        ptr = myblock;
  47.        myblock += size;
  48.        return(ptr);
  49.       }
  50. }
  51.